﻿$(function(){//延迟加载
	var dvbelong='';
	var ipresId='';
	$('a[name=dvres]').click(function(){
		$('#p').height('260px');
		dvbelong=$(this).attr('value');
		var url='showipres?danxuan=dvbelong,'+dvbelong;
		ipdata(url);
	});
	
	$('#searchip').click(function(){
		//alert($('#tt',parent.document).data('options'));
		//parent.addTab('设备查询','dvsec'); 
		$('#p').height('250px');
		var snum=$('#snum').val();
		var tnum=$('#tnum').val();
		var dajuxiang='';
		dajuxiang=$('#dajuxiang').combobox('getValue');
		var url='showipres?';
		//alert(dajuxiang);
		if(dajuxiang=='0'){
			url=url+'&danxuan=dvbelong,'+dvbelong;
		}else{
			url=url+'&danxuan=djxId,'+dajuxiang+':dvbelong,'+dvbelong;
		}				
		if(snum==""&&tnum==""){
			
		}else{
			if(snum==""){
			snum="0";	
			}
			url=url+'&shuzi=availipnum,'+snum+','+tnum;
		}
		//alert(url);
		ipdata(url);		
	});
	
	function ipdata(url){		
		//$('#devres').panel('collapse',true)
		$('#searchres').datagrid({
			width:'auto',
			height:'auto',
			//striped:true,	
			toolbar:'#tb1',
			singleSelect:true,
			url:url,
			async:true,
			method:'get',
			pagination:true,
			rownumbers:true,
			fit:true,
			columns:
			[[
			{"field":"djxname",
			"title":"汇接",
			"width":"8%"
			},
			{
			"field":"iprangebegin",
			"title":"地址段起始",
			"width":"15%"
			},
			{
			"field":"iprangeend",
			"title":"地址段终止",
			"width":"15%"
			},
			{
			"field":"userange",
			"title":"使用范围",
			"width":"8%"
			},
			{
			"field":"ipnum",
			"title":"地址总数",
			"width":"5%"
			},
			{
			"field":"availipnum",
			"title":"可用地址数",
			"width":"5%"
			},
			{
			"field":"describ",
			"title":"描述",
			"width":"10%"
			},
			{
			"field":"ipsort",
			"title":"地址段类型",
			"width":"10%"
			},			
			{
			"field":"maxipnum",
			"title":"最大段地址数",
			"width":"10%"
			},
			{
			"field":"rangestatus",
			"title":"地址段状态",
			"width":"10%"
			}
			]],
			onClickRow:function(index,row){
				$('#content').height('340px');
				ipresId=row.ipresId;
				var url='showipdeploy?ipresId='+ipresId;
				ipdeploy(url);
			},
		onLoadSuccess:function(){
			
		}
		});
		var pager = $('#searchres').datagrid('getPager');	// get the pager of datagrid
		pager.pagination({
			buttons:[{
				iconCls:'icon-search',
				handler:function(){
					var row=$('#searchres').datagrid('getSelected');
					if($('#'+row.oltId).length>0){alert('已显示该设备');}
					else{
					$('#dig').load('dvdetail',{
						dvbelong:$('a[name=dvres]').attr('value'),
						dvId:row.oltId
					},function(){
						$.getScript('/xasj/js/oltdetail.js');
					});	
					}
				}
			},{
				iconCls:'icon-add',
				handler:function(){
					parent.addTab('添加IP资源','toaddipres');
				}
			},{
				iconCls:'icon-edit',
				handler:function(){
					alert('edit');
				}
			}]
		});
	}
	function ipdeploy(url){
		$('#ipdeploy').datagrid({
			width:'auto',
			height:'auto',
			toolbar:'#tb2',
			singleSelect:true,
			//url:'showipdeploy?ipresId='+row.ipresId,
			url:url,
			async:true,
			method:'get',
			pagination:true,
			rownumbers:true,
			fit:true,
			columns:
				[[
				{"field":"djxname",
				"title":"汇接",
				"width":"100px"
				},
				{
				"field":"ipbegin",
				"title":"地址段起始",
				"width":"100px"
				},
				{
				"field":"ipend",
				"title":"地址段终止",
				"width":"100px"
				},
				{
				"field":"gateway",
				"title":"网关",
				"width":"100px"
				},
				{
				"field":"dvbelong",
				"title":"ip类型",
				"width":"70px"
				},
				{
				"field":"status",
				"title":"分配状态",
				"width":"7px"
				},
				{
				"field":"ipdbelong",
				"title":"设备类型",
				"width":"50px"
				},
				{
				"field":"dvname",
				"title":"设备名称",
				"width":"100px"
				},
				{
				"field":"ipcount",
				"title":"地址数",
				"width":"100px"
				},
				{
				"field":"ipsort",
				"title":"地址类型",
				"width":"100px"
				}
				]],
				onClickRow:function(index,row){
					if($('#'+row.dvId).length>0){alert('已显示该设备');}
					else{
					$('#dig').load('dvdetail',{
						dvbelong:row.ipdbelong,
						dvId:row.dvId
					},function(){
						$.getScript('/xasj/js/oltdetail.js');
					});	
					}
				},
				onLoadSuccess:function(){
					
				}
		});
	}
	
	$('#status').combobox({
		onChange:function(){
			var url='';
			if($(this).combobox('getValue')=='0')
				url='showipdeploy?ipresId='+ipresId;
			else
				url='showipdeploy?ipresId='+ipresId+'&status='+$(this).combobox('getValue');
			//alert(url);
			ipdeploy(url);
		}
	});
});